package com.zy.service.impl;

import com.zy.mapper.UserMapper;
import com.zy.entity.User;
import com.zy.service.UserService;
import com.zy.util.NullUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-10-23
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public List<User> selectLikeUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    @Override
    @Transactional
    public int updateLikeUsername(User user) {

        // 参数保护
        String username = user.getUsername();
        if (NullUtil.isBlank(username)) {
            throw new RuntimeException("参数为null或为空");
        }

        // 账号只作为修改条件，而不参与修改
        user.setUsername(null);

        // 主键不参与修改
        user.setId(null);

        // 构建修改条件包装器
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        // 构建修改条件
        updateWrapper.like("username", username);
        // 进行设置
        user.setPhone("6666");
        // 发送修改SQL语句
        return userMapper.update(user, updateWrapper);
    }

    @Override
    @Transactional
    public Page<User> paging(long page, long size) {
        return userMapper.selectPage(new Page<>(page, size), null);
    }
}
